Flash Remoting Article

Introduction to Flash Remoting with .NET


Table of Contents

  1. Introduction
  2. Overview of .NET with Flash Remoting
  3. Connecting ASP.NET to Flash Remoting
  4. Manipulating ASP.NET with Flash

Manipulating ASP.NET with Flash

With the ASP.NET page completed, you can now create a Flash interface to call and manipulate the service.

To start, ensure that you have installed the Flash Remoting Components into Flash. Next, create a new document in Flash MX Professional 2004, select Frame 1 in the Timeline, and open the Actions panel. Here, you must include the NetServices package to implement the Flash Remoting connection and initialize a connection to the ASP.NET page. The NetServices ActionScript file includes several object definitions and methods to carry out the Flash Remoting connection. As an option, you can include a NetDebug ActionScript file to provide additional debugging for your documents. If you haven't installed them yet, download and install the Flash Remoting Components. These components are necessary to use the external NetServices and NetDebug ActionScript files.

#include "NetServices.as"

if( inited == null )
{
	inited = true;
	NetServices.setDefaultGatewayUrl( "http://localhost/flashremoting/gateway.aspx" );
	gatewayConnection = NetServices.createGatewayConnection();
	
	ASPXService = gatewayConnection.getService( "flashremoting", this );
}

This code block creates a connection to the Flash Remoting server and creates a reference to the ASP.NET service. For this method to work, you must create a blank file named gateway.aspx. The URL to this file is passed to the setDefaultGateway. Once the gateway URL is passed, the createGatewayConnection method creates the Flash Remoting server connection. Flash Remoting with .NET requires that you create a physical gateway.aspx file, but other implementations of Flash Remoting do not require a physical gateway file. A best practice is to pass the default gateway as a parameter in the embed tags of your Flash file. This lets you move the Flash Remoting application to a new location without making physical changes to the Flash file. (Note: Upcoming changes in how Internet Explorer reads active content may require confirmation form a user before processing embed tags in Flash. You can find more information on these changes at Macromedia’s Active Content Developer Center.) This code block is packaged in an if statement to prevent Flash Remoting objects from being initialized more than once.

Continuing on in the code above, provide a path to the ASP.NET document. In the follwing line of code:

ASPXService = gatewayConnection.getService( "flashremoting", this );

you set the path to the Flash Remoting Service using dot notation. With ASP.NET pages, provide the directory from the Flash Remoting web server’s root using dot notation. For example, calling an ASP.NET page named http://localhost/flashremoting/cs/services/test.aspx will use the notation flashremoting.cs.services. This implementation will slightly differ for an XML web service or .NET assembly. In our document, the ASP.NET service is located at http://localhost/flashremoting/FlashRemotingService.aspx, making the dot notation for our service simply flashremoting.

With the reference of our .NET service complete, call the ASP.NET page and perform any operations on the page's results. You need to create callback functions that process the result data or provide the status of the method if an error occurs. As a best practice, mask the call to the .NET page with an ActionScript function. This way you can add any last operations or error checking before the service call.

function FlashRemotingService( name )
{
	if( (name != null) && (name != "") )
	{
		ASPXService. FlashRemotingService ( name );
	}
}

function FlashRemotingService_Result( result ) 
{
}

function FlashRemotingService_Status( error )
{
	trace( error.code );
	trace( error.description );
	trace( error.details );
}

The function is the same name as the ASP.NET page. The callback functions described above are created by adding _Result and _Status to the ASP.NET page name. The Flash Player will automatically call the proper callback function when data is received from the ASP.NET service. The result data is automatically transformed to the corresponding ActionScript type for immediate use. Figure 1 shows a detailed list of what .NET types wrap to what Flash Remoting types.

ActionScript to .NET type transformation table
ActionScript Data Type .NET Data Type
Number Int, float, double
Boolean Bool
String String
Array Array
Object Object
Date Date

When the result callback function is called, the Flash application will dynamically produce a text field to display the result string. To ensure the entire text displays in the text field, set the autosize property to true. Edit the FlashRemotingService_Result function in the code example above as follows:

function FlashRemotingService _Result( result )
{
	if( txtResult == undefined )
	{
	    _root.createTextField( "txtResult", 100, 5.7, 87, 160, 18 );
	}
	txtResult.text = result;
	txtResult.autoSize = true;
}

Lastly, you must provide the a form and button to submit a custom message for the application user. To do this, create a text field with type set to input. The text field should have an instance name of txtInputName. Place a label using the text field static type next to the input box with the text name:.

Using the library create a button and place it next to the input text field. Add the following ActionScript to the button:

on( release )
{
	FlashRemotingService( txtInputName.text );
}

This ActionScript on the button will use the onRelease callback function above to submit the name data to the ASP.NET service. Once the ASP.NET page processes the operation, the Flash Player will use the result callback function to display the resulting string to the end user.